package extensions.android;

import java.util.ArrayList;
import java.util.List;

import uclm.extensions.android.R;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

public class InputActivity extends Activity implements SensorEventListener {
	
    private Sensor _currentSensor = null;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.input_activity);
		
		ViewGroup layout = (ViewGroup)findViewById(R.id.input_layout);
		
		_textViews = new TextView[MAX_LINES];
		
		for (int i=0; i<MAX_LINES; i++)
		{
			_textViews[i] = newTextView();
			layout.addView(_textViews[i]);
		}
		
		_sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);

		for (Sensor s : getSensors())
		{
			System.out.println("Sensor:"+getSensorStr(s));
			
			//_sensorManager.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
		}
	}
	
	private void registerSensor(Sensor s)
	{
		_sensorManager.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
	}
	
	private void unregisterSensors()
	{
		_sensorManager.unregisterListener(this);
	}
	
	private List<Sensor> getSensors()
	{
		return _sensorManager.getSensorList(Sensor.TYPE_ALL);
	}
	
	private String getSensorStr(Sensor s)
	{
		return "Name '"+s.getName()+"'; MaxRange '"+s.getMaximumRange()+"'; Power '"+s.getPower()+"'; Res: '"+s.getResolution()+"'; Type:"+s.getType();
	}
	
	private SensorManager _sensorManager;
	
	@Override
	protected void onResume() {
		super.onResume();
		
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		
		finish();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		if (keyCode == KeyEvent.KEYCODE_MENU)
		{
			return false;
		}
		
		addLine("Key down: "+event);
		
		return true;
	}
	
	@Override
	public boolean onKeyLongPress(int keyCode, KeyEvent event) {
		addLine("Key long press:"+event);
		return true;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		addLine("Touch:"+event);
		return true;
	}
	
	@Override
	public boolean onTrackballEvent(MotionEvent event) {
		addLine("TrackBall:"+event);
		return true;
	}
	
	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		addLine("AccuracyChanged of '"+getSensorStr(sensor)+"' Accuracy:"+accuracy);
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		addLine("Sensor changed:"+event);
	}
	
	private TextView newTextView()
	{
		TextView textView = new TextView(this);
		textView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
		textView.setTextAppearance(this, android.R.attr.textAppearanceSmall);
		
		return textView;
	}
	
	private TextView[] _textViews;
	private static final int MAX_LINES = 12;
	private List<String> _lines = new ArrayList<String>();
	
	
	public void addLine(String line)
	{
		
		if (_lines.size() == MAX_LINES)
		{
			_lines.remove(0);
		}
		
		_lines.add(line);
		
		setTextViews();
	}
	
	private void setTextViews()
	{
		int textViewIndex = 0;
		for (String line : _lines)
		{
			if (line == null)
				break;
			
			_textViews[textViewIndex++].setText(line);
			
			if (textViewIndex >= MAX_LINES)
				break;
		}
	}
	
	
	private Sensor getSensorByName(String name)
	{
		for (Sensor s : getSensors())
			if (s.getName().equals(name))
				return s;
		return null;
	}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add("None");
    	
    	for (Sensor s : getSensors())
    		menu.add(s.getName());
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Sensor s = getSensorByName(item.getTitle().toString());
    	
    	unregisterSensors();
    	
    	if (s == null)
    		_currentSensor = null;
    	else
    	{
    		_currentSensor = s;
    		registerSensor(_currentSensor);
    	}
    	
    	return true;
    }

}
